var contentPublicData;
var editContentPublicData;

function publicNav() {
    navSelected('#publcNavItem');
    contentHeadShow('.publicContentHead');
    clearContentTable();
    initPublic();
}

function initPublic() {
    $('#contentData').bootstrapTable({
        data: contentPublicData,
        dataType: 'json',
        pagination: true,
        pageSize: 5,
        striped: true,
        search: false,
        singleSelect: false,
        showHeader: true,
        showFooter: false,
        showColumns: false,
        showRefresh: false,
        showToggle: false,
        sortable: false,
        columns: [{
            field: 'name',
            title: '标题',
            align: 'center',
            valign: 'middle'
        }, {
            field: 'text',
            title: '内容',
            align: 'center',
            valign: 'middle'
        }, {
            field: 'createTime',
            title: '时间',
            align: 'center',
            valign: 'middle'
        },{
            field: 'id',
            title: '操作',
            align: 'center',
            valign: 'middle',
            formatter: publicFormatter
        }]
    });
    initPublicData();
}

function initPublicData() {
    $.ajax({
        async: false,
        cache: false,
        type: 'GET',
        datType: "json",
        accept: "application/json;charset=UTF-8",
        contentType: "application/json;charset=UTF-8",
        url: '/public',
        success: function (data) {
            contentPublicData = data._embedded.public;
            var table = $('#contentData');
            table.bootstrapTable('refreshOptions', {data: contentPublicData, dataType: "json"});
        },
        error: function (data) {
        }
    });
}

function publicFormatter(value, row, index) {
    var id = value;
    var result = "";
    result += "<button type='button' class='btn btn-warning' data-toggle='modal' data-target='#publicUpdate' onclick=\"publicUpdate('" + index + "')\"><i class='fa fa-pencil'></i> 修改</button>";
    result += "<button type='button' class='btn btn-danger' onclick=\"publicDelete('" + id + "')\"><i class='fa fa-trash'></i> 删除</button>";
    return result;
}


function publicQuery() {
    var publicName = $("#publicName").val();
    if (isNull(publicName)) {
        $.ajax({
            async: false,
            cache: false,
            type: 'GET',
            datType: "json",
            accept: "application/json;charset=UTF-8",
            contentType: "application/json;charset=UTF-8",
            url: '/public',
            success: function (data) {
                contentPublicData = data._embedded.public;
                var table = $('#contentData');
                table.bootstrapTable('refreshOptions', {data: contentPublicData, dataType: "json"});
            },
            error: function (data) {
            }
        });
    } else {
        $.ajax({
            async: false,
            cache: false,
            type: 'GET',
            datType: "json",
            accept: "application/json;charset=UTF-8",
            contentType: "application/json;charset=UTF-8",
            url: '/public/search/findByName?name=' + publicName,
            success: function (data) {
                var dataArray = new Array();
                if (!isNull(data)) {
                    dataArray.push(data);
                }
                contentPublicData = dataArray;
                var table = $('#contentData');
                table.bootstrapTable('refreshOptions', {data: contentPublicData, dataType: "json"});
            },
            error: function (data) {
            }
        });
    }
}

function publicAddSave() {
    var data = {};
    data.name = $("#addPublicName").val();
    data.text = $("#addPublicText").val();
    $.ajax({
        async: true,
        cache: false,
        type: 'POST',
        data: JSON.stringify(data),
        datType: "json",
        accept: "application/json;charset=UTF-8",
        contentType: "application/json;charset=UTF-8",
        url: '/public',
        success: function (data) {
            swal('温馨提示', '新增公告成功', 'success');
            initPublicData();
        },
        error: function (data) {
            swal('温馨提示', '新增公告失败', 'error');
        }
    });
}

var editContentPublicData;
function publicUpdate(index) {
    var row = contentPublicData[index];
    editContentPublicData = contentPublicData[index];
    editContentPublicData = row;
    $("#updatePublicName").val(row.name);
    $("#updatePublicText").val(row.text);
}

function publicUpdateSave(){
    var data = {};
    data.id = editContentPublicData.id;
    data.name = $("#updatePublicName").val();
    data.text = $("#updatePublicText").val();
    data.createTime = editContentPublicData.createTime;
    $.ajax({
        async: true,
        cache: false,
        type: 'PUT',
        data: JSON.stringify(data),
        datType: "json",
        accept: "application/json;charset=UTF-8",
        contentType: "application/json;charset=UTF-8",
        url: '/public/' + data.id,
        success: function (data) {
            swal('温馨提示', '修改公告成功', 'success');
            initPublicData();
        },
        error: function (data) {
            swal('温馨提示', '修改公告失败', 'error');
        }
    });
}

function publicDelete(id) {
    $.ajax({
        async: true,
        cache: false,
        type: 'DELETE',
        datType: "json",
        accept: "application/json;charset=UTF-8",
        contentType: "application/json;charset=UTF-8",
        url: '/public/' + id,
        success: function (data) {
            swal('温馨提示', '删除公告成功', 'success');
            initPublicData();
        },
        error: function (data) {
            swal('温馨提示', '删除公告失败', 'error');
        }
    });
}


function publicUpload() {
    var uploadData = new FormData();
    var uploadName = $("#publicUploadFile").val();
    uploadData.append("file", $("#publicUploadFile")[0].files[0]);
    uploadData.append("name", uploadName);
    $.ajax({
        url: '/excel/import',
        type: 'POST',
        async: false,
        data: uploadData,
        // 告诉jQuery不要去处理发送的数据
        processData: false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType: false,
        beforeSend: function () {
            console.log("正在进行，请稍候");
        },
        success: function (data) {
            swal('温馨提示', '导入成功', 'success');
        }
    });
}